home *** CD-ROM | disk | FTP | other *** search
/ Team Palmtops 7 / Palmtops_numero07.iso / WinCE / SDKWindowsCE / HandHeldPCPro30 / sdk.exe / Jupiter SDK / data1.cab / MFC_Samples / chkbook / chkrec.cpp < prev    next >
Encoding:
C/C++ Source or Header  |  1999-02-19  |  5.5 KB  |  233 lines

  1. // CCheckRecord implementation
  2. //
  3. // This is a part of the Microsoft Foundation Classes C++ library.
  4. // Copyright (C) 1999 Microsoft Corporation
  5. // All rights reserved.
  6. //
  7. // This source code is only intended as a supplement to the
  8. // Microsoft Foundation Classes Reference and related
  9. // electronic documentation provided with the library.
  10. // See these sources for detailed information regarding the
  11. // Microsoft Foundation Classes product.
  12.  
  13. #include "stdafx.h"
  14. #include "chkrec.h"
  15. #include "resource.h"
  16.  
  17. CCheckRecord::CCheckRecord() 
  18. {
  19.     m_dwCents = 0;
  20.     m_szPayTo[0] = '\0';
  21.     m_szDate[0] = '\0';
  22.     m_szMemo[0] = '\0';
  23. }
  24.  
  25. void CCheckRecord::Reset() 
  26.     m_dwCents = 0; 
  27.     m_szPayTo[0] = '\0';
  28.     m_szDate[0] = '\0'; 
  29.     m_szMemo[0] = '\0'; 
  30. }
  31.  
  32. void CCheckRecord::GetCheck(DWORD dwCents, CString& strPayTo,
  33.     CString& strDate, CString& strMemo)
  34.     dwCents = m_dwCents; 
  35.     strPayTo = m_szPayTo;
  36.     strDate = m_szDate; 
  37.     strMemo = m_szMemo;
  38. }
  39.  
  40. void CCheckRecord::SetCheck(DWORD dwCents, CString strPayTo,
  41.     CString strDate, CString strMemo)
  42. {    
  43.     m_dwCents = dwCents;
  44.     _tcsncpy(m_szPayTo, strPayTo, sizeof(m_szPayTo)/sizeof(TCHAR) - 1);
  45.     _tcsncpy(m_szDate, strDate, sizeof(m_szDate)/sizeof(TCHAR) - 1);
  46.     _tcsncpy(m_szMemo, strMemo, sizeof(m_szMemo)/sizeof(TCHAR) - 1);
  47.     m_szPayTo[39] = '\0';
  48.     m_szDate[9] = '\0';
  49.     m_szMemo[39] = '\0';
  50. }
  51.  
  52. BOOL CCheckRecord::IsEmpty()
  53. {
  54.     return ((m_dwCents == 0) && (m_szPayTo[0] == '\0') &&
  55.         (m_szDate[0] == '\0') && (m_szMemo[0] == '\0'));
  56. }
  57.  
  58. BOOL CCheckRecord::IsSame(DWORD dwCents, CString strPayTo,
  59.     CString strDate, CString strMemo)
  60. {
  61.     return ((m_dwCents == dwCents) && (_tcscmp(m_szPayTo, strPayTo) == 0) &&
  62.         (_tcscmp(m_szDate, strDate) == 0) && (_tcscmp(m_szMemo, strMemo) == 0));
  63. }
  64.  
  65. // CCheckBookFile implementation
  66. CCheckBookFile::CCheckBookFile()
  67. {
  68.     m_strFileName = _T("");
  69.     m_nRecordLength = 0;
  70.     m_nRecordCount = 0;
  71. }
  72.  
  73. CCheckBookFile::~CCheckBookFile()
  74. {
  75.     m_file.Close();
  76. }
  77.  
  78. BOOL CCheckBookFile::New()
  79. {
  80.     CFileDialog dlg(FALSE, _T("chb"), NULL, OFN_CREATEPROMPT | OFN_HIDEREADONLY, NULL, NULL);
  81.     
  82.     CString allFilter;
  83.     CString strFilter;
  84.     CString strTitle;
  85.     VERIFY(allFilter.LoadString(AFX_IDS_ALLFILTER));
  86.     strFilter += allFilter;
  87.     strFilter += (TCHAR)'\0'; 
  88.     strFilter += _T("*.*");
  89.     strFilter += (TCHAR)'\0'; 
  90.     dlg.m_ofn.nMaxCustFilter++;
  91.     dlg.m_ofn.lpstrFilter = strFilter;
  92.     dlg.m_ofn.lpstrFilter = strFilter;
  93.     strTitle.LoadString(IDS_FILE_NEW);
  94.     dlg.m_ofn.lpstrTitle = strTitle;
  95.     if (dlg.DoModal() == IDCANCEL)
  96.         return FALSE;
  97.  
  98.  
  99.     if (m_file.m_hFile != (UINT)CFile::hFileNull)
  100.         m_file.Close();
  101.     if (!m_file.Open(dlg.GetPathName(), CFile::modeCreate | CFile::modeNoTruncate | CFile::modeReadWrite))
  102.     {
  103.         AfxMessageBox(CString((LPCTSTR)IDS_MESSAGE5));
  104.         // Call open again
  105.         if (!New()) 
  106.             return FALSE;
  107.         else
  108.             return TRUE;
  109.     }
  110.  
  111.     m_strFileName = dlg.GetPathName();
  112.     m_nRecordLength = sizeof(CCheckRecord);
  113.     m_nRecordCount = m_file.GetLength() / m_nRecordLength;
  114.  
  115.     return TRUE;
  116. }
  117.  
  118. BOOL CCheckBookFile::Open()
  119. {
  120.     CFileDialog dlg(TRUE, _T("chb"), NULL, OFN_CREATEPROMPT | OFN_HIDEREADONLY, NULL, NULL);
  121.     
  122.     CString allFilter;
  123.     CString strFilter;
  124.     VERIFY(allFilter.LoadString(AFX_IDS_ALLFILTER));
  125.     strFilter += allFilter;
  126.     strFilter += (TCHAR)'\0'; 
  127.     strFilter += _T("*.*");
  128.     strFilter += (TCHAR)'\0'; 
  129.     dlg.m_ofn.nMaxCustFilter++;
  130.     dlg.m_ofn.lpstrFilter = strFilter;
  131.     if (dlg.DoModal() == IDCANCEL)
  132.         return FALSE;
  133.  
  134.     if (m_file.m_hFile != (UINT)CFile::hFileNull)
  135.         m_file.Close();
  136.     if (!m_file.Open(dlg.GetPathName(), CFile::modeCreate | CFile::modeNoTruncate | CFile::modeReadWrite))
  137.     {
  138.         AfxMessageBox(CString((LPCTSTR)IDS_MESSAGE5));
  139.         // Call open again
  140.         if (!Open()) 
  141.             return FALSE;
  142.         else
  143.             return TRUE;
  144.     }
  145.  
  146.     m_strFileName = dlg.GetPathName();
  147.     m_nRecordLength = sizeof(CCheckRecord);
  148.     m_nRecordCount = m_file.GetLength() / m_nRecordLength;
  149.     return TRUE;
  150. }
  151.  
  152. BOOL CCheckBookFile::WriteRecord(UINT nPos, CCheckRecord* pRecord)
  153. {
  154.     if (nPos >= m_nRecordCount)
  155.         m_nRecordCount++;
  156.     m_file.Seek(nPos * m_nRecordLength, CFile::begin);
  157.     m_file.Write((void*)pRecord, sizeof(CCheckRecord));
  158.     return TRUE;
  159. }
  160.  
  161.  
  162. BOOL CCheckBookFile::ReadRecord(UINT nPos, CCheckRecord* pRecord)
  163. {
  164.     m_file.Seek(nPos * m_nRecordLength, CFile::begin);
  165.     m_file.Read((void*)pRecord, sizeof(CCheckRecord));
  166.     return TRUE;
  167. }
  168.  
  169. // CCheckBook implementation
  170. CCheckBook::CCheckBook() 
  171. {
  172.     Reset();
  173. }
  174.  
  175. void CCheckBook::Reset()
  176. {
  177.     m_check.Reset();
  178.     m_fileCheckBook.Reset();
  179.     m_bExists = FALSE;
  180. }
  181.  
  182. BOOL CCheckBook::New()
  183. {
  184.     BOOL bIsOK = m_fileCheckBook.New();
  185.     if (bIsOK)
  186.     {
  187.         m_bExists = TRUE;    // Not necessarily the opposite
  188.         m_check.Reset();
  189.     }
  190.     return bIsOK;
  191. }
  192.  
  193. BOOL CCheckBook::Open()
  194. {
  195.     BOOL bIsOK = m_fileCheckBook.Open();
  196.     if (bIsOK)
  197.     {
  198.         m_bExists = TRUE;    // Not necessarily the opposite
  199.         m_check.Reset();
  200.     }
  201.     return bIsOK;
  202. }
  203.  
  204. CCheckRecord* CCheckBook::GetCheck(int nCheck)
  205.     m_fileCheckBook.ReadRecord(nCheck - FIRST_CHECK_NO, &m_check);
  206.     return &m_check;
  207. }
  208.  
  209. int CCheckBook::SetCheck(int nCheck, DWORD dwCents, CString strPayTo,
  210.     CString strDate, CString strMemo)
  211. {
  212.     m_check.SetCheck(dwCents, strPayTo, strDate, strMemo);
  213.     m_fileCheckBook.WriteRecord(nCheck - FIRST_CHECK_NO, &m_check);
  214.     return (m_fileCheckBook.GetRecordCount() + FIRST_CHECK_NO);
  215. }
  216.  
  217. BOOL CCheckBook::IsCheckEmpty(int nCheck)
  218. {
  219.     GetCheck(nCheck);
  220.     return m_check.IsEmpty();
  221. }
  222.  
  223. BOOL CCheckBook::IsCheckSame(int nCheck, DWORD dwCents, CString strPayTo,
  224.     CString strDate, CString strMemo)
  225. {
  226.     GetCheck(nCheck);
  227.     return m_check.IsSame(dwCents, strPayTo, strDate, strMemo);
  228. }
  229.  
  230.